package com.yuzhitong.shapi.base;

import android.Manifest;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.DrawableRes;
import androidx.appcompat.app.AppCompatActivity;

import com.yuzhitong.shapi.R;

import me.jessyan.autosize.AutoSize;
import me.jessyan.autosize.AutoSizeConfig;
import me.jessyan.autosize.utils.ScreenUtils;

/**
 * 带标题的activity
 */
public abstract class BaseLayoutActivity extends AppCompatActivity {
    private LinearLayout baseLayout;
    private LinearLayout basePageBox;
    private LinearLayout baseTitle;
    protected View contentView;
    private View status;

    protected static final int REQUEST_PERMISSION_CODE = 1000;
    /**
     * 需要申请的权限数组
     */
    protected String[] needPermissions = {
            // 网络状态
            Manifest.permission.ACCESS_NETWORK_STATE,
            // WIFI状态
            Manifest.permission.ACCESS_WIFI_STATE,
            // 手机信息及通话状态
            Manifest.permission.READ_PHONE_STATE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
        } catch (Exception e) {

        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.base_layout);

        baseLayout = findViewById(R.id.base_layout);
        basePageBox = findViewById(R.id.base_content);
        baseTitle = findViewById(R.id.base_title);

        status = findViewById(R.id.status);
        status.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight()));

        //        Window window = getWindow();
        //        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //        int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        //        window.getDecorView().setSystemUiVisibility(option);
        //        window.setStatusBarColor(Color.TRANSPARENT);
    }

    public void setDarkStatus() {
        //5.0及以上
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN  //设置为全屏
                        //                 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // 忽略导航栏
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//状态栏字体颜色设置为黑色这个是Android 6.0才出现的属性   默认是白色
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);//设置为透明色
        window.setNavigationBarColor(Color.TRANSPARENT);
    }

    public void hideStatus() {
        status.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
    }

    public void setStatusBackground(int color) {
        status.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight()));
        status.setBackgroundColor(color);
    }

    public void setStatusBackgroundResource(int resId) {
        status.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight()));
        status.setBackgroundResource(resId);
    }

    @Override
    public void setContentView(int layoutResID) {
        if (layoutResID == R.layout.base_layout) {
            super.setContentView(layoutResID);
            return;
        }
        contentView = View.inflate(this, layoutResID, null);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        contentView.setLayoutParams(layoutParams);
        basePageBox.addView(contentView);
    }

    public LinearLayout getTitleView() {
        return baseTitle;
    }

    public void setTitleBackgroundResource(@DrawableRes int resId) {
        baseTitle.setBackgroundResource(resId);
    }

    public void setTitleBackgroundColor(int color) {
        baseTitle.setBackgroundColor(color);
    }

    public void setBackgroundColor(int color) {
        baseLayout.setBackgroundColor(color);
    }

    public void setBackgroundResource(int resource) {
        baseLayout.setBackgroundResource(resource);
    }

    /**
     * 获取状态栏高度
     */
    public int getStatusBarHeight() {
        Resources resources = getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    private void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }

    private void showSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
}
